package com.token.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.token.mapper.SysRoleMapper;
import com.token.model.dto.BaseDTO;
import com.token.model.po.SysRole;
import com.token.model.vo.SysRoleVO;
import com.token.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: henriy
 * @Date: 2023/10/25
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public IPage<SysRoleVO> page(BaseDTO baseDTO) {
        Integer pageNum = baseDTO.getPageNum();
        Integer pageSize = baseDTO.getPageSize();
        Page<SysRole> pageParam = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<SysRole> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysRole::getDelFlag, 0);
        IPage<SysRole> rolePage = sysRoleMapper.selectPage(pageParam, queryWrapper);
        IPage<SysRoleVO> resultPage = new Page<>();
        List<SysRoleVO> sysRoleVoList = rolePage.getRecords()
                .stream()
                .map(dto -> {
                    SysRoleVO sysRoleVO = new SysRoleVO();
                    BeanUtils.copyProperties(dto, sysRoleVO);
                    return sysRoleVO;
                }).collect(Collectors.toList());

        return resultPage.setPages(rolePage.getPages())
                .setRecords(sysRoleVoList)
                .setCurrent(rolePage.getCurrent())
                .setSize(rolePage.getSize())
                .setTotal(rolePage.getTotal());
    }


}
